const app = getApp();
var wxbarcode = require('../../utils/orderDetail.js');
const config = require('../../config.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    //托运清单，富文本处理
    var article = app.companyInfo.consignAgreement == null ? 
    '<strong style="color:red">请联系管理员设置托运协议</strong>' : app.companyInfo.consignAgreement;
    WxParse.wxParse('article', 'html', article, this, 5);

    //设置物流公司名称
    this.setData({ company: app.companyInfo })

    //生成二维码    
    wxbarcode.qrcode('qrcode', config.tenantCode + "," + options.orderNumber, 300, 300);

    //查询运单详情并赋值
    getInfo(this, options.orderNumber);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});

/**
 * 获取信息
 */
function getInfo(_this, orderNumber) {
  queryInfo(_this, orderNumber, function (msg) {
    let result = msg.data.content;
    if (undefined != result && typeof result == "object") {
      let real = {};
      for (let t in result) {
        if (result[t] != null && result[t] != "null" && result[t] != "undefined") {
          real[t] = result[t];
        }
      }
      real.consignerAddress = real.consignerAddress != undefined ? real.consignerAddress.replace('⊙', ' ') : "";
      _this.setData({ res: real });
    } else {
      _this.setData({
        showView: false,
        message: "抱歉，没有找到相关数据！",
        imagePath: "../images/noData.png"
      });
    }
  })
}

/**
 * 请求数据
 */
function queryInfo(_this,orderNumber, callback) {
  wx.showLoading({ mask: true, title: "加载中" });
  wx.request({
    url: config.requestUrl + "order/getOrderInfo",
    method: "GET",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"//;charset=utf-8
    },
    data: { "id": "", "code": config.tenantCode, "orderNo": orderNumber },
    success: function (msg) {
      callback(msg);
      wx.hideLoading();
    },
    fail: function (msg) {
      // console.log(msg);
      _this.setData({
        showView: false,
        message: "抱歉，没有找到相关数据！",
        imagePath: "../images/noData.png"
      });
      wx.hideLoading();
    }
  });
}